﻿using NFine.Application.BusinessManage;
using NFine.Code;
using NFine.Domain.Entity.BusinessManage;
using System.Web;
using System.Web.Mvc;

namespace NFine.Web.Areas.BusinessManage.Controllers
{
    public class D_AmountChangeController : ControllerBase
    {
        private D_AmountChangeApp AmountApp = new D_AmountChangeApp();

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string keyword)
        {
            var data = new
            {
                rows = AmountApp.GetList(pagination, keyword),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetAllGridJson(Pagination pagination, string keyword)
        {
            var data = new
            {
                rows = AmountApp.GetAllList(pagination, keyword),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTransGridJson(Pagination pagination, string pData)
        {
            var data = new
            {
                rows = AmountApp.GetTransList(pagination, pData),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGrid(string keyword)
        {
            var data = AmountApp.GetGrid(keyword);
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = AmountApp.GetForm(keyValue);
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetForm(string keyValue)
        {
            string userId = OperatorProvider.Provider.GetCurrent().UserId;
            var data = AmountApp.GetMemberForm(keyValue, userId);
            return Content(data.ToJson());
        }

        /*
        // 系统启用时，带入一些点位，正式启用清理点位的余额，不要删除相关代码
        [HttpGet]
        public JsonResult ClearInitAmount()
        {
            try
            {
                AmountApp.ClearInitAmount();
                return Json("清理成功", JsonRequestBehavior.AllowGet);
            }
            catch (System.Exception ex)
            {
                return Json(ex, JsonRequestBehavior.AllowGet);
            }
        }
        */

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult SubmitForm(D_AmountChangeEntity areaEntity, string keyValue)
        {
            if (areaEntity.F_Money < 100 || areaEntity.F_Money > 100000)
                return Error("输入金额必须大于等于100,小于等于100000");
            if (areaEntity.F_Money%100!=0)
                return Error("输入金额必须为100的整数倍");
            AmountApp.SubmitForm(areaEntity, keyValue);
            return Success("操作成功。");
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult SubTransfer(D_AmountChangeEntity areaEntity, string keyValue)
        {
            var LoginId = OperatorProvider.Provider.GetCurrent().UserCode;
            if(areaEntity.F_Money<=0)
                return Error("输入金额必须大于0");
            if(areaEntity.F_UserId== LoginId)
                return Error("不能给自己转账");
            AmountApp.SubTransfer(areaEntity);
            return Success("操作成功。");
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult CheckForm(D_AmountChangeEntity areaEntity, string keyValue)
        {
            AmountApp.CheckForm(areaEntity, keyValue);
            return Success("操作成功。");
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            D_AmountChangeEntity areaEntity = AmountApp.GetForm(keyValue);
            if(areaEntity.F_State==1)
                return Error("已审核无法删除");
            AmountApp.DeleteForm(keyValue);
            return Success("删除成功。");
        }

        [HttpGet]
        public ActionResult SysIndex()
        {
            return View();
        }

        [HttpGet]
        public ActionResult SysForm()
        {
            return View();
        }
        [HttpGet]
        public ActionResult MemberTransfer()
        {
            return View();
        }
    }
}
